package org.jeecg.modules.inOrder1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.basicSkuDetail.entity.BasicSkuDetail;
import org.jeecg.modules.basicSkuDetail.service.IBasicSkuDetailService;
import org.jeecg.modules.inOrder1.entity.InOrder1;
import org.jeecg.modules.inOrder1.entity.InOrderZi;
import org.jeecg.modules.inOrder1.mapper.InOrderZiMapper;
import org.jeecg.modules.inOrder1.mapper.InOrder1Mapper;
import org.jeecg.modules.inOrder1.service.IInOrder1Service;
import org.jeecg.modules.reportLabel.entity.ReportLabel;
import org.jeecg.modules.reportLabel.service.IReportLabelService;
import org.jeecg.modules.system.util.ThreadPoolManager;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 入库单
 * @Author: jeecg-boot
 * @Date: 2024-01-04
 * @Version: V1.0
 */
@Service
public class InOrder1ServiceImpl extends ServiceImpl<InOrder1Mapper, InOrder1> implements IInOrder1Service {

    @Autowired
    private InOrder1Mapper inOrder1Mapper;
    @Autowired
    private InOrderZiMapper inOrderZiMapper;
    @Autowired
    private IReportLabelService reportLabelService;
    @Autowired
    private IBasicSkuDetailService skuDetailService;
    @Autowired
    private ThreadPoolManager threadPoolManager;

    @Override
    @Transactional
    public void saveMain(InOrder1 inOrder1, List<InOrderZi> inOrderZiList) {
        inOrder1Mapper.insert(inOrder1);
        if (inOrderZiList != null && inOrderZiList.size() > 0) {
            for (InOrderZi entity : inOrderZiList) {
                //外键设置
                entity.setPid(inOrder1.getId());
                Double length = entity.getLength();
                Double width = entity.getWidth();
                Double height = entity.getHeight();
                if (length != null && width != null && height != null){
                    entity.setVolume(length*width*height);
                }
                inOrderZiMapper.insert(entity);
            }
            Runnable task = () -> {
                try {
                    addLWHOfSku(inOrderZiList);
                } catch (Exception e) {
                    log.error("线程池调用【InOrder1ServiceImpl.saveMain】方法出现异常", e);
                }
            };
            threadPoolManager.executeTask(task);

        }
    }

    private void addLWHOfSku(List<InOrderZi> inOrderZiList) {
        for(InOrderZi inOrderZi : inOrderZiList){
            String sku = inOrderZi.getSkuCode1().replaceAll(" ", "");
            int index = sku.indexOf("-");
            String skuSubstring = index != -1 ? sku.substring(0, index + 4) : sku;
            LambdaQueryWrapper<BasicSkuDetail> wrapper = Wrappers.lambdaQuery();
            wrapper.like(BasicSkuDetail::getSkuCode, skuSubstring);
            BasicSkuDetail skuBySkuCode = skuDetailService.getOne(wrapper);
            Double length = skuBySkuCode.getLength();
            Double width = skuBySkuCode.getWidth();
            Double high = skuBySkuCode.getHigh();
            boolean isUpdated = false;
            if (length == null){
                skuBySkuCode.setLength(inOrderZi.getLength());
                isUpdated = true;
            }
            if (width == null){
                skuBySkuCode.setWidth(inOrderZi.getWidth());
                isUpdated = true;
            }
            if (high == null){
                skuBySkuCode.setHigh(inOrderZi.getHeight());
                isUpdated = true;
            }
            if (isUpdated) {
                skuDetailService.updateById(skuBySkuCode);
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(InOrder1 inOrder1, List<InOrderZi> inOrderZiList) {

        inOrder1Mapper.updateById(inOrder1);

        //1.先删除子表数据
        inOrderZiMapper.deleteByMainId(inOrder1.getId());

        //2.子表数据重新插入
        if (inOrderZiList != null && inOrderZiList.size() > 0) {
            for (InOrderZi entity : inOrderZiList) {
                //外键设置
                entity.setPid(inOrder1.getId());
                Double length = entity.getLength();
                Double width = entity.getWidth();
                Double height = entity.getHeight();
                if (length != null && width != null && height != null){
                    entity.setVolume(length*width*height);
                }
                inOrderZiMapper.insert(entity);
            }
        }

        // 异常终止
        if (inOrder1.getInOrderStatus().equals(13)) {
            LambdaQueryWrapper<ReportLabel> reportLabelLqw = Wrappers.lambdaQuery(ReportLabel.class);
            reportLabelLqw.eq(ReportLabel::getInOrderId, inOrder1.getId());
            reportLabelService.remove(reportLabelLqw);
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        inOrderZiMapper.deleteByMainId(id);
        inOrder1Mapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            inOrderZiMapper.deleteByMainId(id.toString());
            inOrder1Mapper.deleteById(id);
        }
    }

    @Override
    public Integer getInOrderCount() {
        return inOrder1Mapper.getInOrderCount(new Date());
    }

}
